//copy from ngx

#ifndef MY_ATOMIC_H
#define MY_ATOMIC_H


#include <config.h>
#include <core.h>



        
#if (MSGD_HAVE_SCHED_YIELD)
#define msgd_sched_yield()  sched_yield()
#else
#define msgd_sched_yield()  usleep(1)
#endif


#if ( __i386__ || __i386 )

typedef int32_t                     msgd_atomic_int_t;
typedef uint32_t                    msgd_atomic_uint_t;
typedef volatile msgd_atomic_uint_t  msgd_atomic_t;
#define MSG_ATOMIC_T_LEN            (sizeof("-2147483648") - 1)

#define MSG_HAVE_ATOMIC_OPS  1

#include "gcc_atomic_x86.h"

#endif




#if !(MSG_HAVE_ATOMIC_OPS)

#define MSG_HAVE_ATOMIC_OPS  0

typedef int32_t                     msgd_atomic_int_t;
typedef uint32_t                    msgd_atomic_uint_t;
typedef volatile msgd_atomic_uint_t  msgd_atomic_t;
#define MSG_ATOMIC_T_LEN            (sizeof("-2147483648") - 1)


static msgd_inline msgd_atomic_uint_t
msgd_atomic_cmp_set(msgd_atomic_t *lock, msgd_atomic_uint_t old,
     msgd_atomic_uint_t set)
{
     if (*lock == old) {
         *lock = set;
         return 1;
     }

     return 0;
}


static msgd_inline msgd_atomic_int_t
msgd_atomic_fetch_add(msgd_atomic_t *value, msgd_atomic_int_t add)
{
     msgd_atomic_int_t  old;

     old = *value;
     *value += add;

     return old;
}

#define msgd_memory_barrier()
#define msgd_cpu_pause()

#endif


void msgd_spinlock(msgd_atomic_t *lock, msgd_atomic_int_t value, uint32_t spin);

#define msgd_trylock(lock)  (*(lock) == 0 && msgd_atomic_cmp_set(lock, 0, 1))
#define msgd_unlock(lock)    *(lock) = 0


#endif /* MY_ATOMIC_H */
